<?php
/**
 * Created by PhpStorm.
 * User: tengwei
 * Date: 2018/11/16
 * Time: 10:17 AM
 */

namespace common\sources\read\businessadvice;

use common\models\craftsman\BusinessAdvice;

class BusinessAdviceAccess implements IBusinessAdviceAccess
{
    public function ShopAdvice($data,$count = false) {
        $query = BusinessAdvice::find()
            ->select("business_advice.id,bus_id,advice,create_time,content,phone,qq,wx,business_advice.imgUrl")
            ->leftJoin('buyuser','business_advice.bus_id = buyuser.id');
        if (!empty($data['shopId'])) {
            $query = $query->where(['bus_id' => $data['shopId']]);
        }
        if (!empty($data['begin_time'])) {
            $query = $query->andWhere([
                '>',
                'create_time',
                $data['begin_time']
            ]);
        }
        if (!empty($data['end_time'])) {
            $query = $query->andWhere([
                '<',
                'create_time',
                $data['end_time']
            ]);
        }
        if (!$count) {
            $list = $query->offset(($data['page']-1) * $data['limit'])
                ->limit($data['limit'])
                ->asArray()
                ->all();
            return $list;
        }
        $result = $query
            ->asArray()
            ->count();
        return $result;
    }

    //删除商家建议
    public function DelShopAdvice($data) {
        $advice = BusinessAdvice::findOne(['id' => $data['id']]);
        if ($advice) {
            if ($advice->delete()) {
                return true;
            }
        }
        return false;
    }
}